package com.rgs.se.data.stack;


import com.rgs.se.data.array.Array;

/**
 * @author Ren Gaoshuai
 * @ClassName ArrayStack
 * @date 2022-04-03 17:53
 **/
public class ArrayStack<E> implements Stack<E> {

    Array<E> array;

    public ArrayStack() {
        this(10);
    }

    public ArrayStack(int capacity) {
        array = new Array<>(capacity);
    }

    @Override
    public int getSize() {
        return array.getSize();
    }

    @Override
    public boolean isEmpty() {
        return array.isEmpty();
    }

    @Override
    public void push(E o) {
        array.add(o);
    }

    @Override
    public E pop() {
        return array.delete(array.getSize() - 1);
    }

    @Override
    public E peek() {
        return array.getLast();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Stack:[");
        for (int i = 0; i < array.getSize(); i++) {
            builder.append(array.index(i));
            if (i != array.getSize() - 1) {
                builder.append(", ");
            }
        }
        builder.append("] top");
        return builder.toString();
    }
}
